package com.mygdx.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.maps.MapRenderer;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.IsometricTiledMapRenderer;
import com.badlogic.gdx.math.Vector2;

/**
 * Created by 80002023 on 2016/7/27.
 */
public class MapScaleScreen implements Screen, GestureDetector.GestureListener {

    private OrthographicCamera mCamera;
    private MapRenderer mMapRenderer;

    @Override
    public boolean touchDown(float x, float y, int pointer, int button) {
        return false;
    }

    @Override
    public boolean tap(float x, float y, int count, int button) {
        return false;
    }

    @Override
    public boolean longPress(float x, float y) {
        return false;
    }

    @Override
    public boolean fling(float velocityX, float velocityY, int button) {
        return false;
    }

    @Override
    public boolean pan(float x, float y, float deltaX, float deltaY) {
        mCamera.position.add(-deltaX, deltaY, 0);
        return false;
    }

    @Override
    public boolean panStop(float x, float y, int pointer, int button) {
        return false;
    }

    @Override
    public boolean zoom(float initialDistance, float distance) {
        mCamera.zoom = initialDistance / distance;
        return false;
    }

    @Override
    public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
        return false;
    }

    @Override
    public void pinchStop() {

    }

    @Override
    public void show() {

        TiledMap map = new TmxMapLoader().load("isometric_grass_and_water.tmx");

        // 镜头需要设定宽高
        mCamera = new OrthographicCamera(800, 480);
        // 这里默认1个unit单元=1个pixels像素
        // IsometricTiledMapRenderer还可以设置单元和像素之间的比例
        mMapRenderer = new IsometricTiledMapRenderer(map);
        // 设置镜头的宽和高分别是800,480单元,y方向向上和镜头中间位置
        mCamera.setToOrtho(false, 800, 480);
        mCamera.update();
        Gdx.input.setInputProcessor(new GestureDetector(this));
    }

    @Override
    public void render(float delta) {
        delta = Math.min(0.06f, Gdx.graphics.getDeltaTime());

        // 镜头的更新与设置矩阵到SpriteBatch
        mCamera.update();

        // 设置背景
        Gdx.gl.glClearColor(0.7f, 0.7f, 1.0f, 1);
        // 清屏
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        // 设置摄像机
        mMapRenderer.setView(mCamera);
        // 地图绘制
        mMapRenderer.render();
    }

    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {

    }
}
